package com.yt.dor.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.dor.models.dto.DormBedDTO;
import com.yt.dor.models.service.IDormBedService;
import com.yt.dor.models.vo.DormBuildingVO;
import com.yt.dor.models.vo.DormRoomVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.yt.dor.models.service.IDormRoomService;
import com.yt.dor.models.bo.DormRoomSaveBO;
import com.yt.dor.models.bo.DormRoomBO;
import com.yt.dor.models.dto.DormRoomDTO;

/**
 * 宿舍房间表控制器
 *
 * @author thekai
 * @Date 2025-10-21 14:13:41
 */

@RestController
@RequestMapping("/cms/dormRoom")
@Slf4j
@Api(value = "DormRoomController",tags = "宿舍房间表接口")
public class DormRoomController {

    @DubboReference
    private IDormRoomService dormRoomService;
    @DubboReference
    private IDormBedService dormBedService;

    @ApiOperation(value = "添加宿舍房间表",notes = "添加宿舍房间表")
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody DormRoomSaveBO saveBO) {
        boolean flag = dormRoomService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存宿舍房间表",notes = "保存宿舍房间表")
    public Result save(DormRoomSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改宿舍房间表",notes = "修改宿舍房间表")
    @PostMapping("/update")
    public Result update(@RequestBody DormRoomSaveBO saveBO){
        DormRoomDTO dto = new DormRoomDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = dormRoomService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         DormRoomDTO dto = new DormRoomDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = dormRoomService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除宿舍房间表",notes = "删除宿舍房间表")
    @ApiImplicitParam(name = "id",value = "宿舍房间表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = dormRoomService.deleteById(id);
        //房间删除时，删除床位信息
        Wrapper<DormBedDTO>wrapper = new EntityWrapper<>();
        wrapper.eq("room_id",id);
        dormBedService.delete(wrapper);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除宿舍房间表",notes = "批量删除宿舍房间表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        dormRoomService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<DormRoomDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        DormRoomDTO dto = dormRoomService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/queryDormRoom")
    @ApiOperation(value = "查询宿舍信息", notes = "查询宿舍信息")
    public Result<List<DormRoomVO>> queryDormRoom() {
        List<DormRoomVO> dormRoomVOS = dormRoomService.queryDormRoom();
        return new Result<>(dormRoomVOS);
    }
    @ApiOperation(value = "分页查询宿舍房间表列表",notes = "分页查询宿舍房间表列表")
    @PostMapping("/queryPageList")
    public Result<Page<DormRoomDTO>> queryRoomPageList(@RequestBody DormRoomBO bo){
        // 验证分页参数
        if (bo.getPageNo() == null || bo.getPageNo() < 1) {
            bo.setPageNo(1);
        }
        if (bo.getPageSize() == null || bo.getPageSize() < 1 || bo.getPageSize() > 100) {
            bo.setPageSize(10);
        }
        Wrapper<DormRoomDTO> wrapper = new EntityWrapper<>();
        if(bo.getBuildingId()!=null)
            wrapper.eq("building_id",bo.getBuildingId());
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getRoomNumber() != null){
            wrapper.like("room_number", bo.getRoomNumber().toString());
        }
        if (bo.getProfessionId() != null){
            wrapper.eq("profession_id", bo.getProfessionId());
        }
        if(bo.getRoomStatus() != null){
            wrapper.eq("room_status", bo.getRoomStatus());
        }
        if(bo.getIsMix() != null){
            wrapper.eq("is_mix", bo.getIsMix());
        }
        Page<DormRoomDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = dormRoomService.selectPage(page, wrapper);
        return new Result<>(page);
    }


}
